<?php
/*
*	$version: 2009-5-29 14:01:40 beta 1.0
*	$id: Ubb.class.php 
*	$author: taoFeng
*	$mailto: font99@Gmail.com
*	$license: This is not a freeware
*/

if (!defined('IN_LEKO')) {header("HTTP/1.0 403 Forbidden");exit;}

class Ubb
{
	private $allowUbbcode		= true;
	private $allowMultimedia	= true;
	private $allowSmile			= true;
	public static $smileDir;
	public static $debug		= false;

	private $validate;

	public function __construct()
	{
		self::$smileDir = "res" . DIRECTORY_SEPARATOR . "img" . DIRECTORY_SEPARATOR . "smiles" . DIRECTORY_SEPARATOR;
		if (!is_dir(FILE_ROOT . self::$smileDir)) {
			exit ("ubb smiles Dir could not be found.");
		}
	}

	public function parseUbb($strUbbtext)
	{
		if ($this->allowUbbcode) {
			$pattern = array(
				"/\[(url)\]\s*(www.|https?:\/\/|ftp:\/\/|gopher:\/\/|news:\/\/|telnet:\/\/|rtsp:\/\/|mms:\/\/|callto:\/\/|ed2k:\/\/){1}([^\[\"']+?)\s*\[\/url\]/is",
				"/\[(url)=www.([^\[\"']+?)\](.+?)\[\/url\]/is",
				"/\[(url)=(https?|ftp|gopher|news|telnet|rtsp|mms|callto|ed2k){1}:\/\/([^\[\"']+?)\](.+?)\[\/url\]/is",
				"/\[(email)]\s*([a-z0-9\-_.+]+)@([a-z0-9\-_]+[.][a-z0-9\-_.]+)\s*\[\/email]/is",
				"/\[(email)=\s*([a-z0-9\-_.+]+)@([a-z0-9\-_]+[.][a-z0-9\-_.]+)](.*?)\[\/email]/is",
				"/\[(img)\]([^\[\"']+?)\[\/img]/is",
				"/\[(img)=([^\[\"']+?)]\[\/img]/is",
				"/\[(color)=(#?[a-zA-Z0-9\._ \-]+)](.+?)\[\/color]/is",
				"/\[(size)=(\d+?)\](.+?)\[\/size]/is",
				"/\[(size)=(\d+(px|pt|in|cm|mm|pc|em|ex|%)+?)](.+?)\[\/size]/is",
				"/\[(font)=([^\[\<]+?)\](.+?)\[\/font]/is"
			);
			if ($this->allowMultimedia) {
				$media_pattern = array(
					"/\[(swf)=(\d{1,3}),(\d{1,3}):([yn]{1})\](.+?)\[\/swf\]/is",
					"/\[(rm)=(\d{1,3}),(\d{1,3}):([yn]{1})\](.+?)\[\/rm\]/is",
					"/\[(wm)=(\d{1,3}),(\d{1,3}):([yn]{1})\](.+?)\[\/wm\]/is",
					"/\[(qt)=(\d{1,3}),(\d{1,3}):([yn]{1})\](.+?)\[\/qt\]/is"
				);

				$pattern = array_merge($pattern,$media_pattern);
			}
			if ($this->allowSmile) {
				$smile_pattern = array(
					"/\[([a-z]+)=(\d{1,3})\]/is"
				);
				
				$pattern = array_merge($pattern,$smile_pattern);
			}
			return preg_replace_callback($pattern,array('self', 'ubbCallback'),$strUbbtext);
		}
		else
		{
			return $strUbbtext;
		}
	}

	public static function cutUrl($url) {
		$length = 65;
		$urllink = "<a href=\"".(strncasecmp($url,"www.",4) == 0 ? "http://$url" : $url).'" target="_blank" class="bbcode_url">';
		if(strlen($url) > $length) {
			$url = substr($url, 0, intval($length * 0.5)).' ... '.substr($url, - intval($length * 0.3));
		}
		$urllink .= $url.'</a>';
		return $urllink;
	}

	public static function parseUrl($_url) {

		$hyperlink = !Validate::_isValidURL($_url[2]) ? self::cutUrl($_url[3]) : "<a href=\"http://www.{$_url[2]}\" target=\"_blank\" class=\"bbcode_url\">{$_url[2]}</a>";

		if (!empty($_url[4])) {
			$hyperlink = "<a href=\"{$_url[2]}://{$_url[3]}\" target=\"_blank\" class=\"bbcode_url\">{$_url[4]}</a>";
		}
		
		return $hyperlink;
	}

	public static function parseEmail($_email) {
		$email = $_email[2].'@'.$_email[3];
		
		if (!Validate::_isEmail($email)) {
			return $_email[0];
		}

		return "<a href=\"mailto:$email\" class=\"bbcode_email\">" . (empty($_email[4]) ? $email : $_email[4]) . "</a>";
	}

	public static function parseMedia($_media) {
		$type = fileExt(basename($_media[5]));
		$play = strcasecmp($_media[4],"y") == 0 ? true : false;
		$w = intval($_media[2]);
		$h = intval($_media[3]);
		
		if (Validate::_isValidURL($_media[5]))
		{
			if(in_array($type, array('ra', 'rm', 'wma', 'wmv', 'mp3', 'mov', 'swf'))) {
				$url = str_replace(array('<', '>'), '', str_replace('\\"', '\"', $_media[5]));
				$mediaid = 'media_'.randomString(3);
				switch($type) {
					case 'ra'	: return '<object classid="clsid:CFCDAA03-8BE4-11CF-B84B-0020AFBBCCFA" width="'.$w.'" height="32"><param name="autostart" value="'.$play.'" /><param name="src" value="'.$url.'" /><param name="controls" value="controlpanel" /><param name="console" value="'.$mediaid.'_" /><embed src="'.$url.'" type="audio/x-pn-realaudio-plugin" controls="ControlPanel" '.($play ? 'autostart="true"' : '').' console="'.$mediaid.'_" width="'.$w.'" height="32"></embed></object>';break;
					case 'rm'	: return '<object classid="clsid:CFCDAA03-8BE4-11cf-B84B-0020AFBBCCFA" width="'.$w.'" height="'.$h.'"><param name="autostart" value="'.$play.'" /><param name="src" value="'.$url.'" /><param name="controls" value="imagewindow" /><param name="console" value="'.$mediaid.'_" /><embed src="'.$url.'" type="audio/x-pn-realaudio-plugin" controls="IMAGEWINDOW" console="'.$mediaid.'_" width="'.$w.'" height="'.$h.'"></embed></object><br /><object classid="clsid:CFCDAA03-8BE4-11CF-B84B-0020AFBBCCFA" width="'.$w.'" height="32"><param name="src" value="'.$url.'" /><param name="controls" value="controlpanel" /><param name="console" value="'.$mediaid.'_" /><embed src="'.$url.'" type="audio/x-pn-realaudio-plugin" controls="ControlPanel" '.($play ? 'autostart="true"' : '').' console="'.$mediaid.'_" width="'.$w.'" height="32"></embed></object>';break;
					case 'wma'	: return '<object classid="clsid:6BF52A52-394A-11d3-B153-00C04F79FAA6" width="'.$w.'" height="64"><param name="autostart" value="'.$play.'" /><param name="url" value="'.$url.'" /><embed src="'.$url.'" autostart="'.$play.'" type="audio/x-ms-wma" width="'.$w.'" height="64"></embed></object>';break;
					case 'wmv'	: return '<object classid="clsid:6BF52A52-394A-11d3-B153-00C04F79FAA6" width="'.$w.'" height="'.$h.'"><param name="autostart" value="'.$play.'" /><param name="url" value="'.$url.'" /><embed src="'.$url.'" autostart="'.$play.'" type="video/x-ms-wmv" width="'.$w.'" height="'.$h.'"></embed></object>';break;
					case 'mp3'	: return '<object classid="clsid:6BF52A52-394A-11d3-B153-00C04F79FAA6" width="'.$w.'" height="64"><param name="autostart" value="'.$play.'" /><param name="url" value="'.$url.'" /><embed src="'.$url.'" autostart="'.$play.'" type="application/x-mplayer2" width="'.$w.'" height="64"></embed></object>';break;
					case 'mov'	: return '<object classid="clsid:02BF25D5-8C17-4B23-BC80-D3488ABDDC6B" width="'.$w.'" height="'.$h.'"><param name="autostart" value="'.($play ? 'true' : 'false').'" /><param name="src" value="'.$url.'" /><embed controller="true" width="'.$w.'" height="'.$h.'" src="'.$url.'" autostart="'.($play ? 'true' : 'false').'"></embed></object>';break;
					case 'swf' : return '<object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" codebase="http://fpdownload.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=7,0,0,0" width="'.$w.'" height="'.$h.'" align="top"><param name="allowScriptAccess" value="sameDomain"><param name="movie" value="'.$url.'"><param name="quality" value="high"><param name="bgcolor" value="#FFFFFF"><param name="play" value="'.($play ? 'true' : 'false').'"><embed src="'.$url.'" width="'.$w.'" height="'.$h.'" play="'.($play ? 'true' : 'false').'" align="top" quality="high" bgcolor="#ffffff" allowScriptAccess="sameDomain" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer"/></object>';break;
				}
			}
		}
		return $_media[0];
	}

	public static function parseImage($_image) {

		$image = $_image[2];

		if (preg_match("/\\.(?:gif|jpeg|jpg|jpe|png)$/", $image)) {
			if (preg_match("/^[a-zA-Z0-9_][^:]+$/", $image)) {
				// No protocol, so the image is in our local image directory, or somewhere under it.
				if (!preg_match("/(?:\\/\\.\\.\\/)|(?:^\\.\\.\\/)|(?:^\\/)/", $image)) {
					return $_image[0];
				}
			}
			else if (Validate::_isValidURL($image, false)) {
				// Remote URL, or at least we don't know where it is.
				return "<img src=\"" . htmlspecialchars($image) . "\" alt=\""
					. htmlspecialchars(basename($image)) . "\" class=\"bbcode_img\" />";
			}
		}

		return $_image[0];
	}

	public static function parseColor($_color) {
		return empty($_color[3]) ? $_color[0] : "<span style='color:$_color[2]'>$_color[3]</span>";
	}

	public static function parseSize($_size) {
		$size = $_size[2];
		$c = count($_size);

		if ($c == 4) {
			switch ($size) {
				case '0': $size = '.5em'; break;
				case '1': $size = '.67em'; break;
				case '2': $size = '.83em'; break;
				default:
				case '3': $size = '1.0em'; break;
				case '4': $size = '1.17em'; break;
				case '5': $size = '1.5em'; break;
				case '6': $size = '2.0em'; break;
				case '7': $size = '2.5em'; break;
			}
		}
		else if ($c == 5)
		{
			$_size[3] = $_size[4];
		}
		return empty($_size[3]) ? $_size[0] : "<span style=\"font-size:$size\">$_size[3]</span>";
	}

	public static function parseFont($_font) {
		return empty($_font[3]) ? $_font[0] : "<span style=\"font-size:$_font[2]\">$_font[3]</span>";
	}

	public static function parseSmile($_smile) {
		$file = self::$smileDir . $_smile[1] . DIRECTORY_SEPARATOR . $_smile[2] . ".gif";

		if (!is_array($GLOBALS['_C']->_getCache(md5($file)))) {
			if (file_exists(FILE_ROOT . $file)) {
				$GLOBALS['_C']->_setCache(md5($file),"1");
				return "<img src=\"/" . $file . "\" border=\"0\"\/>";
			}
		}
		else {
			return "<img src=\"/" . $file . "\" border=\"0\"\/>";
		}

		return $_smile[0];
	}

	public static function ubbCallback($_m)
	{
		if (self::$debug)
			print_r($_m);

		if (is_array($_m))
		{
			switch(strtolower($_m[1]))
			{
				case "url" :
					return self::parseUrl($_m);
				case "email" :
					return self::parseEmail($_m);
				case "img" :
					return self::parseImage($_m);
				case "color" :
					return self::parseColor($_m);
				case "size" :
					return self::parseSize($_m);
				case "font" :
					return self::parseFont($_m);
				case "swf" :
					return self::parseMedia($_m);
				case "rm" :
					return self::parseMedia($_m);
				case "wm" :
					return self::parseMedia($_m);
				case "qt" :
					return self::parseMedia($_m);
				default :
					return self::parseSmile($_m);
					break;
			}
		}
	}
}
?>
